package com.yangjun.service.realtimesource;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Maps;
import com.yangjun.common.DVConstants;
import com.yangjun.common.realtimesource.ChartDefine;
import com.yangjun.dao.impl.realtimeLiveAnalysis.realtimeSource.WebImpl;
import com.yangjun.utils.DvDateUtils;

@Service
public class RealtimeSourceService {
	
	@Autowired
    WebImpl mModel;
	/*
     * Brief: PC / Wap / 微信  平台流量数据
     */
    public Map<String, Object> getWebData(String platforms, String contrastday, String today) {
    	
    	Map<String, Object> result = Maps.newHashMap();
    	
    	String yestoday = DvDateUtils.getDate(new Date(), -1);
    	
    	List<Map<String, Object>> dataList = mModel.getPlatformOverview(today, yestoday,contrastday, Arrays.asList(platforms));
    	if (dataList.size() == 1) {
    		result.put("yesterday", null);
		   	result.put("today", dataList.get(0));
    	} else if (dataList.size() > 1) {
    		result.put("yesterday", dataList.get(0));
		   	result.put("today", dataList.get(1));
	   	} else {
	   		result.put("yesterday", null);
	   		result.put("today", null);
	   	}
    	
    	// 获取平台流量折线图
    	String[] lineCharts = ChartDefine.getPlatformLineCharts();
    	for (int i = 0; i < lineCharts.length; i ++) {
    		result.put(lineCharts[i], getLineChartMap(lineCharts[i], today, contrastday, Arrays.asList(platforms), null, null, null));
    		
    	}
    	
    	return result;
    }
    
    /**
     * 获取平台流量折线图
     * @param yAxis
     * @param today
     * @param contrastday
     * @param ordersources
     * @param firstChannel
     * @param secondChannel
     * @param landpage
     * @return
     */
    public Map<String, Object> getLineChartMap(String yAxis, String today, String contrastday, List<String> ordersources, String firstChannel, String secondChannel, String landpage) {
    	String[] todayHourDataArr = new String[24];
        String[] contrastdayHourDataArr = new String[24];
        Map<String,Object> itemMap = Maps.newHashMap();
        List<Map<String,Object>> querylist = null;
        
        querylist = mModel.getLineChartData(yAxis, today, contrastday, ordersources, firstChannel, secondChannel, landpage); 
        for(Map<String,Object> map : querylist){
            String day = String.valueOf(map.get("dataDate"));
            String hour = String.valueOf(map.get("dataHour"));
            if (day.equals(today)) {
                todayHourDataArr[Integer.parseInt(hour)] = String.valueOf(map.get(yAxis));
            } else {
            	contrastdayHourDataArr[Integer.parseInt(hour)] = String.valueOf(map.get(yAxis));
            }
        }
        
        itemMap.put("name", ChartDefine.getPlatformLineChartDesc(yAxis));
        itemMap.put("legend",DVConstants.REALTIME_CHART_LEGEND);
        itemMap.put("xAxis", DVConstants.TWENTY_FOUR_HOURS_XAXIS);
        itemMap.put("today", todayHourDataArr);
        itemMap.put("contrastday", contrastdayHourDataArr);
        return itemMap;
    }
    

}
